using Geek.Server.Core.Actors;

namespace Server.Logic.WebServer
{
    /// <summary>
    /// 中心服 服务器信息管理
    /// </summary>
    public partial class WebServerCompAgent
    {
        [Service]
        public virtual Task<HttpResponse> RequestAddServer(WebAddServerInfoReq req)
        {
            if (req.ServerInfo == null || !req.ServerInfo.IsValid())
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));
            
            if (State.ServerMap.ContainsKey(req.ServerInfo.Id))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ServerInfoExists));
            
            State.ServerMap[req.ServerInfo.Id] = req.ServerInfo;
            return Task.FromResult(HttpResponse.Create());
        }
        
        [Service]
        public virtual Task<HttpResponse> RequestUpdateServer(WebUpdateServerInfoReq req)
        {
            if (req.ServerInfo == null || !req.ServerInfo.IsValid())
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ParametersError));
            
            if (!State.ServerMap.ContainsKey(req.ServerInfo.Id))
                return Task.FromResult(HttpResponse.Create((int)MessageErrorCode.ServerInfoNotExists));
            
            State.ServerMap[req.ServerInfo.Id] = req.ServerInfo;
            return Task.FromResult(HttpResponse.Create());
        }

        [Service]
        public virtual Task<WebQueryServerListResp> RequestQueryServerList(string ip, WebQueryServerListReq req)
        {
            var resp = new WebQueryServerListResp();
            resp.ServerInfos ??= new List<ServerInfo>();
            resp.ServerInfos.AddRange(State.ServerMap.Values);

            if (req.AccountId != 0 && State.AccountMap.TryGetValue(req.AccountId, out var accountInfo) && 
                accountInfo.RoleMap != null && accountInfo.RoleMap.Count > 0)
                resp.RoleMap = accountInfo.RoleMap;
            
            return Task.FromResult(resp);
        }
    }
}